import {
  Controller,
  Post,
  UseInterceptors,
  UploadedFile,
  Body,
  BadRequestException,
} from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
import {
  ApiOperation,
  ApiTags,
  ApiConsumes,
  ApiBody,
  ApiBearerAuth,
} from '@nestjs/swagger'
import { FileUploadDto } from './dto/file.dto'
import { FileService } from './file.service'
import { CurrentUser } from 'src/common/decorator/user.decorator'
import { UserEntity } from 'src/entities/user.entity'
// import path from 'path'
const path = require('path')

@Controller('file')
@ApiTags('file')
export class FileController {
  constructor(private readonly FileService: FileService) {}

  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  @ApiConsumes('multipart/form-data')
  @ApiOperation({ summary: '单文件上传' })
  @ApiBearerAuth()
  @ApiBody({
    type: FileUploadDto,
  })
  async uploadFile(@UploadedFile() file) {
    const [err, path] = await this.FileService.save(file)
    if (!err) {
      return path
    } else {
      throw new BadRequestException('文件上传失败')
    }
  }
}
